<?php

class DeleteImageAction extends CAction
{
    public function run()
    {
        if(!Yii::app()->request->isPostRequest || !isset($_GET['id']))    
            throw new CHttpException(400,'the request is invalid');
    
        if($_GET['id']!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the kitchen not belong to you');
    
        $kitchen=Kitchen::loadObjectById($_GET['id']);
        if($kitchen===null)
            throw new CHttpException(404,'The requested kitchen does not exist.');
        if($kitchen->id!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the kitchen not belong to you');

        $succ=false;
        if(!$kitchen->image){
            $succ=true;
        }
        else {
            $image = $kitchen->image;
            $kitchen->image = UserImage::$null;
            if(Kitchen::saveObject($kitchen, array('image','updateTime'))) {
                UserImage::delete('kitchen',$image);
                $succ=true;
            }
        }

        $this->controller->renderPartial('deleteImage',array('succ'=>$succ, 'kitchen'=>$kitchen), false, true);
    }
}
  
?>